﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ACM.UserControls;
using BusinessLogic;
using Model;

namespace ACM
{
    public partial class ChangeRequest : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["stat"] != null && Request.QueryString["stat"] == "succ_req")
                {
                    Status.ShowSuccess("ส่งคำร้องขอเปลี่ยน Dealer สำเร็จ กรุณารอการยืนยันจาก Dealer ที่ท่านได้ส่งคำร้องไปถึง");
                }


                GetRequest();
            }
        }

        private void GetRequest()
        {
            SearchRequestCommand cmd = new SearchRequestCommand(CurrentUser.dealer_code, rdoList.SelectedValue);
            cmd.Execute();

            if (rdoList.SelectedValue == "REQ_BY_U")
            {
                grdYourRequest.DataSource = null;
                grdYourRequest.DataSource = cmd.Result;
                grdYourRequest.DataBind();
                mtvTab.ActiveViewIndex = 0;
            }
            else
            {
                grdRequestToYou.DataSource = null;
                grdRequestToYou.DataSource = cmd.Result;
                grdRequestToYou.DataBind();
                mtvTab.ActiveViewIndex = 1;

                if (cmd.Result != null && cmd.Result.Count > 0)
                    btnChangeReqSubmit.Visible = true;
            }
        }

        protected void rdoList_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetRequest();
        }

        protected void grdYourRequest_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                acm_req req = (acm_req)e.Row.DataItem;
                if (req.change_status == "ยอมรับ")
                {
                    LinkButton lb = (LinkButton)e.Row.FindControl("lnkKeyIn");
                    lb.Visible = true;
                }
            }
        }

        protected void btnChangeReqSubmit_Click(object sender, EventArgs e)
        {
            UpdateStatusCommand cmd;
            
            foreach (GridViewRow row in grdRequestToYou.Rows)
            {
                ucYesNo uc = (ucYesNo)row.FindControl("ucAgreement");

                if (uc != null && uc.Value != null)
                {
                    cmd = new UpdateStatusCommand(uc.Value,CurrentUser.id,uc.tx_id);

                    cmd.Execute();
                }
            }

            GetRequest();

            Status.ShowSuccess("อัพเดทสถานะสำเร็จ");
        }

        protected void grdYourRequest_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "KeyIn")
            {
                Response.Redirect("SaleKeyIn.aspx?id=" + e.CommandArgument);
            }
        }
    }
}